package com.example.demo.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.demo.entity.*;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import com.example.demo.utils.Jwt;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2020-11-28
 */
@RestController
@RequestMapping("/user")
@CrossOrigin(origins="http://localhost:8080")
@Slf4j
public class UserController {
    @Autowired
    UserService user;
    @GetMapping ("/get_userid")
    public Object getAll()
    {
        return user.list();
    }


    @PostMapping("/login")
    public Map<String,Object> login(@RequestBody User user1) {
        Map<String, Object> map = new HashMap<>();
        List<User> list = user.list();
        int i=0;
        for (User c : list) {
            i++;
            if (c.getUsername().equals(user1.getUsername()) && c.getPassword().equals(user1.getPassword()))
            {
                Map<String,String >payload=new HashMap<>();
                payload.put("username",user1.getUsername());
                String token= Jwt.getToken(payload);
                map.put("state","true");
                map.put("user_id",c.getId());
                map.put("token",token);
                map.put("username",c.getUsername());
                return map;
            }
            else if(i==list.size()-1)
            {
               map.put("state","false");
               map.put("msg","登录失败");
               return map;
            }

        }
        return map;
    }

    @PostMapping("/username")
    public Map<String,Object> test(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        String token =request.getHeader("token");
        map.put("state",true);
        map.put("msg","请求成功");
        DecodedJWT verify= Jwt.verify(token);
        System.out.println(verify.getClaim("username").asString());
        return  map;
    }

     @Autowired
     UserMapper user1;
    @GetMapping("/getlovebook")
    public  Object getlovebook(Integer id)
     {

         return user1.getlovebook(id);
     }

     @PostMapping("/collect")
    public Object collect(@RequestBody collect collect)
     {

         if (user1.getfavor(collect.getBook_id(),collect.getUser_id()) != null) {
             return "true";
         }

        else
         return "false";
     }
     @GetMapping("/add")
     public void add_Collection(Integer user_id,Integer book_id){


        user1.add(user_id,book_id);
     }

     @GetMapping("/remove")
    public  void remove(Integer user_id,Integer book_id)
     {
         user1.remove(book_id,user_id);
     }
//   @GetMapping("/test")
//    public Object test()
//   {
//       return user1.getfavor();
//   }
}